/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUICambiarContrasenha.java
 *
 * Created on Oct 19, 2011, 6:33:23 PM
 */
package GUI;


import BE.*;
import BL.BLContrasenha;
import BL.BLUsuario;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author hunter2409
 */
public class GUICambiarContrasenha extends javax.swing.JDialog {

    /** Creates new form GUICambiarContrasenha */
    BEUsuario objUsuario;
    BEContrasenha objContrasenha;
    BLContrasenha objGestorContrasenha;
    BLUsuario objGestorUsuario;
    int intentos=0;

    public GUICambiarContrasenha(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        objGestorContrasenha= new BLContrasenha();
        objGestorUsuario= new BLUsuario();
        initComponents();
    }

    public GUICambiarContrasenha(java.awt.Frame parent, boolean modal,BEUsuario user) {
        super(parent, modal);
        objUsuario= user;
        objGestorContrasenha= new BLContrasenha();
        objGestorUsuario= new BLUsuario();
        this.setTitle("Cambiar Contraseña - "+objUsuario.getUsuario());
        
        initComponents();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCancelar = new javax.swing.JButton();
        lbContrasenhaAntigua = new javax.swing.JLabel();
        btnAceptar = new javax.swing.JButton();
        lbConfirmarContrasenhaNueva = new javax.swing.JLabel();
        lbContrasenhaNueva = new javax.swing.JLabel();
        txtConfirmarNuevaContrasenha = new javax.swing.JPasswordField();
        txtNuevaContrasenha = new javax.swing.JPasswordField();
        txtContrasenhaAntigua = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btnCancelar.setText("Cancelar");
        btnCancelar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnCancelar.setMinimumSize(new java.awt.Dimension(100, 25));
        btnCancelar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        lbContrasenhaAntigua.setText("Contraseña Actual:");

        btnAceptar.setText("Aceptar");
        btnAceptar.setMaximumSize(new java.awt.Dimension(100, 25));
        btnAceptar.setMinimumSize(new java.awt.Dimension(100, 25));
        btnAceptar.setPreferredSize(new java.awt.Dimension(100, 25));
        btnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAceptarActionPerformed(evt);
            }
        });

        lbConfirmarContrasenhaNueva.setText("Confirmar Contraseña:");

        lbContrasenhaNueva.setText("Nueva Contraseña:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbConfirmarContrasenhaNueva)
                            .addComponent(lbContrasenhaNueva)
                            .addComponent(lbContrasenhaAntigua))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtConfirmarNuevaContrasenha, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                            .addComponent(txtNuevaContrasenha)
                            .addComponent(txtContrasenhaAntigua))))
                .addContainerGap(10, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbContrasenhaAntigua)
                    .addComponent(txtContrasenhaAntigua, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lbContrasenhaNueva)
                    .addComponent(txtNuevaContrasenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbConfirmarContrasenhaNueva)
                    .addComponent(txtConfirmarNuevaContrasenha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAceptarActionPerformed

    objGestorContrasenha.cargar();
    objGestorUsuario.cargar();
    int result=JOptionPane.showConfirmDialog(null,"Desea actualizar los datos del usuario?", "Edicion de Usuario",    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    
    if (result==JOptionPane.YES_OPTION)
    {
        
        
        objContrasenha= objGestorContrasenha.obtenerContrasenhaActualIdUsuario(objUsuario.getIdUsuario());
        intentos = objGestorUsuario.obtenerIntentosFallidos(objUsuario);
        System.out.println(intentos);
        if(txtContrasenhaAntigua.getText().equals(objContrasenha.getContrasenha()))
        {

            if ((txtNuevaContrasenha.getText()).equals(txtConfirmarNuevaContrasenha.getText()))
            {

                if ((objGestorContrasenha.validarContrasenha(txtNuevaContrasenha.getText()))&&(objGestorContrasenha.validarUltimasContrasenhas(txtNuevaContrasenha.getText(),objUsuario)))
                {
                   objContrasenha.setContrasenha(txtNuevaContrasenha.getText());
                   objContrasenha.setEstado("ACTIVO");
                   Date fecha = new Date();
                   objContrasenha.setFechaCreacion(fecha);
                   objContrasenha.setIdUsuario(objUsuario.getIdUsuario());

                   objGestorContrasenha.actualizar(objContrasenha);
                   this.dispose();
                }
                else
                {
                JOptionPane.showMessageDialog(null,"Escriba una contraseña valida, considere los parametros de Seguridad del Sistema.","Cambiar Contraseña - "+objUsuario.getUsuario(),JOptionPane.ERROR_MESSAGE);
                }
            }
            else
            {
                JOptionPane.showMessageDialog(null,"Las contraseñas nuevas no coinciden.","Cambiar Contraseña - "+objUsuario.getUsuario(),JOptionPane.ERROR_MESSAGE);
            }

        }
        else
        {
            JOptionPane.showMessageDialog(null,"La contraseña actual no es valida.","Cambiar Contraseña - "+objUsuario.getUsuario(),JOptionPane.ERROR_MESSAGE);
            objGestorUsuario.actualizarIntentosFallidos(objUsuario, intentos+1);
        }
       
        
    }
    
    
    
}//GEN-LAST:event_btnAceptarActionPerformed

private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
    this.dispose();
}//GEN-LAST:event_btnCancelarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUICambiarContrasenha.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUICambiarContrasenha.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUICambiarContrasenha.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUICambiarContrasenha.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                GUICambiarContrasenha dialog = new GUICambiarContrasenha(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAceptar;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JLabel lbConfirmarContrasenhaNueva;
    private javax.swing.JLabel lbContrasenhaAntigua;
    private javax.swing.JLabel lbContrasenhaNueva;
    private javax.swing.JPasswordField txtConfirmarNuevaContrasenha;
    private javax.swing.JPasswordField txtContrasenhaAntigua;
    private javax.swing.JPasswordField txtNuevaContrasenha;
    // End of variables declaration//GEN-END:variables
}
